package quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <h1></h1>
 *
 * @author zhh 2019-06-03
 */
public class HelloScheduler {
    public static void main(String[] args) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob").build();
        // 开始时间：获取距离当前时间3秒后的时间
        Date startTime = new Date();
        startTime.setTime(startTime.getTime() + 3000);
        // 结束时间：获取距离当前时间3秒后的时间
        Date endTime = new Date();
        endTime.setTime(endTime.getTime() + 6000);
        // 创建一个 Trigger 实例，定义该 job 立即执行，并且每隔2秒钟重复执行一次，直到永远
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "group1")
                .startAt(startTime)
                .endAt(endTime)
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                                .withIntervalInSeconds(2)
                                .repeatForever()
                ).build();
        // 创建 Schedule 实例
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        // 打印当前的执行时间
        System.out.println("Current Exec Time is: " + LocalDateTime.now());
        scheduler.scheduleJob(jobDetail, trigger);
    }
}
